const app=getApp();
var that;
const db = wx.cloud.database();
var util = require('../../../utils/util.js');
Page({
  data: {
    itemArray:[
      ["姓名","赵云雷"],
      ["学号","18061119"],
      ["专业","计算机科学与技术"],
      ["年龄","22岁"],
      ["工作经历","C++、Java、Python"]
    ],
    userInfo:{},
    content:"",
    comments:{}
  },


  /**
   * 返回首页按钮所用功能
   */
  returnindex: function (options) {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },

  async savecomment(content){
    db.collection('zylcomment').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        content: content,
        user: app.globalData.userInfo,
        date:util.formatTime(new Date())
      },
      success: function(res) {
        that.data.content = "";
        that.setData({
          content: '',
        })
        wx.showToast({
          title: '发表评论成功',
        })
      },
    })
  },

  async commentSubmit(e){
    
    var content=e.detail.value['input-content'];
    that.content=content;
    if(content.trim()!=''){
      wx.showLoading({
        title: '正在发表评论',
        mask: true
      })
      if(await app.hasUserInfo()){
        await that.savecomment(content);
        that.getData();
        wx.hideLoading()
      }
      else{
        wx.navigateTo({
          url: '/pages/login/login'
        })
      }
    }
    else{
      wx.showToast({
        title:"说点什么吧",
      })
    }
    
  },

  /**
   * 获取填写的内容
   */
  getTextAreaContent: function(event) {
    app.content = event.detail.value;
  },

  onLoad: function () {
    that = this;
    this.getData();
  },

  async getData() {
    await wx.cloud.callFunction({
      name:"getcomment"
    }).then(res=>{
      that.data.comments = res.result.data;
      that.setData({
        comments: that.data.comments,
      })
    })
  },
})